import fetch from '@system.fetch';
import router from '@system.router';
import qs from 'querystring'; //npm install qs，npm install querystring
import prompt from '@system.prompt'; // 导入弹窗模块
import http from '@ohos.net.http';

var timer

export default {
    data: {
        timer: '',
        num: 0,
        up_to_standard: 0,
        not_up_to_standard: 0,
        degree_completion: '',
        score: "80",
        pieSeg: {
            value:10

        },
    },


    onShow() {
        this.timer = setInterval(this.pieDraw, 1000) // 每个1000毫秒执行 drawPie 函数
        console.info('使用饼状图数据更新定时器')
    },
    onHide() {

        clearInterval(timer)
        console.info('停止饼状图数据更新定时器')
    },

    pieDraw() {
//    onPieDraw() {
//    onInit() {
//        console.info(this.data.pieSeg[0].value)
//        var degree_completion = Number(this.data.degree_completion)
//        var up_to_standard = Number(this.data.up_to_standard)
//        var not_up_to_standard = Number(this.data.not_up_to_standard)
//
//        degree_completion = up_to_standard + not_up_to_standard
//        this.data.degree_completion = degree_completion
//
//        this.data.pieSeg[0].value = up_to_standard
//        this.data.pieSeg[1].value = not_up_to_standard
//        fetch.fetch({
////            url: "http://127.0.0.1:8000/api/manageDegreeCompletion/", // 路径，明文网络流量http
////            url: "http://ciygme.natappfree.cc/api/manageDegreeCompletion/", // 路径，明文网络流量http
//            url: "http://124.71.225.45/api/manageDegreeCompletion/", // 路径，明文网络流量http
////            data: qs.stringify({
//////                'sendName': "张三",
////                'sendScore': "80",
////            }),
////            data: qs.stringify({
//////                'sendUsername': this.username,
////                'sendScore': this.score,
////            }),
//            data: qs.stringify({
//                'sendScore': "80"
//            }),
//            //验证，转为字符串发给后台
//            responseType: "json", //请求的参数类型
//            method: "POST",
//            success: (resp) => {
////                console.log("返回的数据：" + JSON.parse(resp)) //日志中打印出数据
//                var getresult = JSON.parse(resp.data); // 令获取到的数据赋给winfo
//                console.log("返回的数据：" + resp.data) // 日志中打印出数据
//                var up_to_standard = Number(getresult[0].up_to_standard) // 转化为Number类型
////                console.log("返回的数据：" + JSON.parse(up_to_standard)) //日志中打印出数据
//                var not_up_to_standard = Number(getresult[0].not_up_to_standard) // 转化为Number类型
//
////                console.log("返回的数据：" + getresult); //打印出数据
//
//                // 更新图表
//                var degree_completion = up_to_standard + not_up_to_standard
////                this.data.degree_completion = degree_completion
//                this.degree_completion = degree_completion
//
////                this.data.pieSeg[0].value = Number(up_to_standard)
////                this.data.pieSeg[1].value = not_up_to_standard
//
//                console.info("接收后：" + up_to_standard)
//                console.info("接收后：" + not_up_to_standard)
//
////                this.data.pieSeg[0].value = up_to_standard
////                this.pieSeg[0].value = up_to_standard
//
////                const el = this.$element('data_pie')
////                e.target.dataSet.a = this.data.pieSeg[0].value
////                var a= e.target.dataSet.a
////                console.info("11111111111"+a)
//
////                console.info("接收前：" + this.data.pieSeg[0].value)
////                this.$set('pieSeg[0].value', up_to_standard)
////                console.info("接收后：" + this.data.pieSeg[0].value)
////                this.$set('pieSegvalue',not_up_to_standard)
////                console.info("接收后：" + this.data.pieSeg[1].value)
//
////                return this.data.pieSeg[0].value = up_to_standard
////                this.data.pieSeg.value = up_to_standard
////                console.info('lllllllllllllllll'+this.data.pieSeg.value)
//
//                // 动态更新数据
////                this.data.up_to_standard = up_to_standard
////                this.data.not_up_to_standard = up_to_standard + not_up_to_standard
//
//                // ！！！此处写成this.up_to_standard而非this.data.up_to_standard！！！
//                this.up_to_standard = up_to_standard
//                this.not_up_to_standard = up_to_standard + not_up_to_standard
//
////                console.info("11111:" + this.data.up_to_standard)
////                console.info("11111:" + this.data.not_up_to_standard)
//
//                console.info("22222:" + this.up_to_standard)
//                console.info("22222:" + this.not_up_to_standard)
//            },
//            fail: (resp) => {
////                this.winfo = resp.data;
//                console.log("获取数据失败！")
//                console.log(JSON.stringify(resp))
//            }
//        });

        // 每一个httpRequest对应一个http请求任务，不可复用
        let httpRequest = http.createHttp();
        // 用于订阅http响应头，此接口会比request请求先返回。可以根据业务需要订阅此消息
        // 从API 8开始，使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
        httpRequest.on('headersReceive', (header) => {
            console.info('header: ' + JSON.stringify(header));
        });
        httpRequest.request(
            // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
            "http://124.71.225.45/api/manageDegreeCompletion/",
            {
                //                method: http.RequestMethod.POST, // 可选，默认为http.RequestMethod.GET
                method: 'POST', // 可选，默认为http.RequestMethod.GET
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json'
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: {
                    //                    "data": "data to send",
                    'sendScore': "80",
                },
                connectTimeout: 60000, // 可选，默认为60s
                readTimeout: 60000, // 可选，默认为60s
            }, (err, data) => {
            if (!err) {
                //                console.log("返回的数据：" + JSON.parse(resp)) //日志中打印出数据
                var getresult = JSON.parse(data.result); // 令获取到的数据赋给winfo
                console.log("返回的数据：" + data.result) // 日志中打印出数据
                var up_to_standard = Number(getresult[0].up_to_standard) // 转化为Number类型
                //                console.log("返回的数据：" + JSON.parse(up_to_standard)) //日志中打印出数据
                var not_up_to_standard = Number(getresult[0].not_up_to_standard) // 转化为Number类型

                //                console.log("返回的数据：" + getresult); //打印出数据

                // 更新图表
                var degree_completion = up_to_standard + not_up_to_standard
                //                this.data.degree_completion = degree_completion
                this.degree_completion = degree_completion

                //                this.data.pieSeg[0].value = Number(up_to_standard)
                //                this.data.pieSeg[1].value = not_up_to_standard

                console.info("接收后：" + up_to_standard)
                console.info("接收后：" + not_up_to_standard)

                //                this.data.pieSeg[0].value = up_to_standard
                //                this.pieSeg[0].value = up_to_standard

                //                const el = this.$element('data_pie')
                //                e.target.dataSet.a = this.data.pieSeg[0].value
                //                var a= e.target.dataSet.a
                //                console.info("11111111111"+a)

                //                console.info("接收前：" + this.data.pieSeg[0].value)
                //                this.$set('pieSeg[0].value', up_to_standard)
                //                console.info("接收后：" + this.data.pieSeg[0].value)
                //                this.$set('pieSegvalue',not_up_to_standard)
                //                console.info("接收后：" + this.data.pieSeg[1].value)

                //                return this.data.pieSeg[0].value = up_to_standard
                //                this.data.pieSeg.value = up_to_standard
                //                console.info('lllllllllllllllll'+this.data.pieSeg.value)

                // 动态更新数据
                //                this.data.up_to_standard = up_to_standard
                //                this.data.not_up_to_standard = up_to_standard + not_up_to_standard

                // ！！！此处写成this.up_to_standard而非this.data.up_to_standard！！！
                this.up_to_standard = up_to_standard
                this.not_up_to_standard = up_to_standard + not_up_to_standard

                //                console.info("11111:" + this.data.up_to_standard)
                //                console.info("11111:" + this.data.not_up_to_standard)

                console.info("22222:" + this.up_to_standard)
                console.info("22222:" + this.not_up_to_standard)
            } else {
                console.log("获取数据失败！")
                console.log(JSON.stringify(data))

                // 当该请求使用完毕时，调用destroy方法主动销毁。
                httpRequest.destroy();
            }
        });
    },

    changeNum() {
        this.num++;
    },
    ToCheck() {
        router.push({
            uri: "pages/manage/check/index",
        });
    },
    ToFall() {
        router.push({
            uri: "pages/manage/falling/index",
        })
    },
    ToMedicineSupervise() {
        router.push({
            uri: "pages/manage/medicine_supervise/medicine_supervise"
        })
    },
    ToData() {
        router.push({
            uri: "pages/manage/data_detail/index",
        });
    },
}
